// useReducer:可以更好的管理一个状态的多种复杂操作
import { useReducer } from "react"
function countReducer(state,action){
    switch(action.type){
        case "+":
            return state+1
        case "-":
            return state-1
        default:
            throw new Error()
    }
}
export default function UseReducer(){
    const [state,dispatch] = useReducer(countReducer,0)
    const handleIncrement = ()=>{dispatch({type:"-"})}
    const handleDecrement = ()=>{dispatch({type:"+"})}
    return (
        <div style={{padding:10}}>
        <button onClick={handleIncrement}>-</button>
        <span> {state} </span>
        <button onClick={handleDecrement}>+</button>
        </div>
    )
}